/*
 * @Copyright: EasyCompany 2021
 * @Author: asterisk
 * @Date: 2021-02-05 16:48:17
 * @Description: 
 */

import 'package:date_time_format/date_time_format.dart';

enum EasyLogLevelInternal {
  /// 最冗余的
  trace,

  /// 调试级别
  debug,

  /// 普通级别
  info,

  /// 警告级别
  warning,

  /// 错误级别
  error,

  /// 致命级别
  fatal,
}

/// 日志控制管理
/// 是否打印im日志，可以专注调试，生产环境建议开启
/// 设置im打印tag，这样可以解决tag冲突问题
///
class EasyLogInternal {
  /// 日志总开关
  static bool isPrintLog = false;

  /// 日志根tag
  static String tag = '[test]';

  /// 日志级别
  static EasyLogLevelInternal level = EasyLogLevelInternal.info;

  static void tracePrint(Object object) {
    imPrint(EasyLogLevelInternal.trace, object);
  }

  static void debugPrint(Object object) {
    imPrint(EasyLogLevelInternal.debug, object);
  }

  static void infoPrint(Object object) {
    imPrint(EasyLogLevelInternal.info, object);
  }

  static void warningPrint(Object object) {
    imPrint(EasyLogLevelInternal.warning, object);
  }

  static void errorPrint(Object object) {
    imPrint(EasyLogLevelInternal.error, object);
  }

  static void fatalPrint(Object object) {
    imPrint(EasyLogLevelInternal.fatal, object);
  }

  static void imPrint(EasyLogLevelInternal level, Object object) {
    if (true == isPrintLog) {
      print(_timeToString() + _levelToString(level) + tag + ' ' + object);
    }
  }

  static void imOriginalPrint(Object object) {
    if (true == isPrintLog) {
      print(object);
    }
  }

  static String _levelToString(EasyLogLevelInternal level) {
    switch (level) {
      case EasyLogLevelInternal.trace:
        return '[trace]';
      case EasyLogLevelInternal.debug:
        return '[debug]';
      case EasyLogLevelInternal.info:
        return '[info]';
      case EasyLogLevelInternal.warning:
        return '[warning]';
      case EasyLogLevelInternal.error:
        return '[error]';
      case EasyLogLevelInternal.fatal:
        return '[fatal]';
      default:
        return '';
    }
  }

  static String _timeToString() {
    final dtl = DateTime.now().toLocal();
    var dtlf = dtl.format('Ymd-His.v');
    return '[$dtlf]';
  }
}
